package com.echoesnet.echoesshortvideo.Repositories;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.text.TextUtils;

import com.echoesnet.echoesshortvideo.Api.HttpMethods;
import com.echoesnet.echoesshortvideo.Api.RequestHeader;
import com.echoesnet.echoesshortvideo.Api.Subscriber.SilenceSubscriber;
import com.echoesnet.echoesshortvideo.AppController;
import com.echoesnet.echoesshortvideo.Constant.Api;
import com.echoesnet.echoesshortvideo.Persistence.AppDatabase;
import com.echoesnet.echoesshortvideo.Persistence.TinyVideoInfoDao;
import com.echoesnet.echoesshortvideo.Persistence.TinyVideoInfoEntity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.orhanobut.logger.Logger;

import java.util.List;
import java.util.Map;

import io.reactivex.Observer;

/**
 * Copyright (C) 2018 科技发展有限公司
 * 完全享有此软件的著作权，违者必究
 * 负责数据处理
 *
 * @author ben
 * @version 1.0
 * @modifier
 * @createDate 2018/4/19 18:12
 * @description
 */
//@Singleton
public class TinyVideoInfoRepository
{
    private final String TAG = TinyVideoInfoRepository.class.getSimpleName();

    private final TinyVideoInfoDao infoDao;

    //@Inject
    public TinyVideoInfoRepository()
    {
        this.infoDao = AppDatabase.getInstance(AppController.getInstance()).tinyVideoInfoDao();
    }

    public LiveData<List<TinyVideoInfoEntity>> getListByUpdate() {
        return infoDao.loadAllByUpdate();
    }




    public LiveData<TinyVideoInfoEntity> getTinyVideoInfo(final String videoID) {
        final MutableLiveData<TinyVideoInfoEntity> data = new MutableLiveData<>();
        Map<String, Object> reqMap = RequestHeader.getCommonPartOfParam();
        reqMap.put("", "");
        HttpMethods.getInstance().startServerRequest(new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                TinyVideoInfoEntity infoBean = new Gson().fromJson(response, TinyVideoInfoEntity.class);
                infoDao.save(infoBean);
            }
        }, Api.GET_USER_INFO, reqMap, false);

        return infoDao.load(videoID);
    }


    public void getHomePageVideoList(int start, int step) {
        Map<String, Object> reqMap = RequestHeader.getCommonPartOfParam();
//        reqMap.put("00", "");
//        reqMap.put("02", "");
        reqMap.put(RequestHeader.INDEX, start);
        reqMap.put(RequestHeader.NUM, start + step);
        HttpMethods.getInstance().startServerRequest(new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response) {
                super.onNext(response);
                Logger.t(TAG).d(response);
                if (TextUtils.isEmpty(response))
                    return;
                TinyVideoInfoEntity[] infoBeans = new Gson().fromJson(
                        response,
                        new TypeToken<TinyVideoInfoEntity[]>() {
                        }.getType()
                );
                String[] strings = new String[infoBeans.length];
                for (int i = 0; i < infoBeans.length; i++) {
                    strings[i] = infoBeans[i].getVideoId();
                }
                if(start==0){ infoDao.deleteByIds(strings); }
                infoDao.insertAll(infoBeans);
            }
        }, Api.INDEX_C_INDEX, reqMap, false);
    }


    public void getHomePageVideoList(int start, int step, MutableLiveData<Boolean> loadmore) {
        Map<String, Object> reqMap = RequestHeader.getCommonPartOfParam();
//        reqMap.put("00", "");
//        reqMap.put("02", "");
        reqMap.put(RequestHeader.INDEX, start);
        reqMap.put(RequestHeader.NUM, start + step);
        HttpMethods.getInstance().startServerRequest(new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response) {
                super.onNext(response);
//                Logger.t(TAG).json(response);
                TinyVideoInfoEntity[] infoBeans = new Gson().fromJson(
                        response,
                        new TypeToken<TinyVideoInfoEntity[]>() {
                        }.getType()
                );
                String[] strings = new String[infoBeans.length];
                for (int i = 0; i < infoBeans.length; i++) {
                    strings[i] = infoBeans[i].getVideoId();
                }
                if(start==0){ infoDao.deleteByIds(strings); }
                infoDao.insertAll(infoBeans);
                loadmore.postValue(infoBeans.length!=0);
            }
        }, Api.INDEX_C_INDEX, reqMap, false);
    }

    public void deleteVideoInfo(String videoID) {
        new Thread(()->{
            TinyVideoInfoEntity tinyVideoInfoEntity = new TinyVideoInfoEntity();
            tinyVideoInfoEntity.setVideoId(videoID);
            infoDao.delete(tinyVideoInfoEntity);
        }).start();
    }

    public void checkVideo(String videoId ,Observer<String> observer)
    {
        Map<String, Object> reqMap = RequestHeader.getCommonPartOfParam();
        reqMap.put(RequestHeader.VIDEO_ID, videoId);

        HttpMethods.getInstance().startServerRequest(observer, Api.VIDEOC_VIDEOSTATUS, reqMap, true);

    }

}
